<?php 
session_start();

$user = $_SESSION['password'];
if (!$user) { header('location: login.php'); exit; }

$sort = $_GET['sort'];
$filter = $_POST['filter_field']; if (!$filter) { $filter = $_GET['filter_field'];}
$filter_data = $_POST['dataa']; if (!$filter_data) { $filter_data = $_GET['dataa'];}
include "../includes/functions.php";
echo db_connect();

if ($_GET['msg'] == 'd1') $msg = "Playlist Deleted";
if ($_GET['msg'] == 'd2') $msg = "Error deleting playlist";
 
 /// put auth check here

if ($_GET['del']) {

  $del_pl =  $_GET['del'];
  if ($result = mysql_query('DELETE FROM playlists WHERE playlist_id = "'.$del_pl.'"') ) {header('location:playlists.php?msg=d1');} else {header('location:playlists.php?msg=d2');}
}


 
$query="SELECT show_id, name from shows WHERE status = 1 ORDER BY name";
$result= mysql_query($query);

 while ($row= mysql_fetch_array($result)) {
 		$show_id=$row['show_id'];
		$show_name=$row['name'];
		// put selected if it's today, now logic here
		$select .= "<option value='$show_id'>$show_name</option>";
 }


 ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Playlists</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function validate()  {
	if (document.form1.show_id.value == 0) {
	alert ("Please select a show to create a playlist for");
	return false;
	} else {  return true; }
 
}  

</script>
<script type="text/javascript" src="../calendar_picker.js"></script>      
</head>

<body bgcolor="#eeeeee">
<a href='playlists.php' style='font-weight:bold;'>«Back to Playlists</a><br><br> 
<form id="form1" name="form1" method="post" action="playlist_new.php" onsubmit="return validate();">
<table width="750" height="60" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td class="index_table_hed" colspan="3">Create a New Playlist</td>
  </tr>
  <tr>
    <td colspan='2'>
      For
      <select name="show_id" id="select">
      <option value="0">-- choose a show --</option>
      <?=$select?>
      </select> &nbsp;&nbsp;on &nbsp;&nbsp;
      <input name="new" type="hidden" value="1" />
<input type='text' size='10' name='new_date' class="input_s" onclick="displayDatePicker('new_date');" value="<?php echo date('Y-m-d') ?>">
</td>
    <td width="350"><input type="submit" name="button" id="button" value="Create Playlist" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
 </form>


<p><?=$msg?></p>

<table width="750" height="60" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="5" class="index_table_hed"><div align="center">FUTURE PLAYLISTS</div></td>
  </tr>
<?php 

$todayDate = date("Y-m-d H:i:s");

$query="SELECT * FROM playlists WHERE datetime > '$todayDate' ORDER BY datetime DESC LIMIT 500";
$result= mysql_query($query);

while ($row= mysql_fetch_array($result)) {
$show_id = $row['show_id'];
$playlist_id = $row['playlist_id'];
$datetime = dateformat ($row['datetime'],"",1);

	$show_query = 'select name from shows where show_id = "'.$show_id.'"';
	$show_result= mysql_query($show_query);
	$show_row= mysql_fetch_array($show_result);
	$show_name = $show_row[0];

	$item_query = 'select playlist_item_id from playlist_items where playlist_id = "'.$playlist_id.'"';
	$item_result= mysql_query($item_query);
	$e=0;
	while ($item_row= mysql_fetch_array($item_result))  {
	$item = $item_row[0];
	$e++;
	}

if ($e){ $class = "index_table"; $delBut = ""; } else { $class = "index_table_empty";  $delBut = "<font color='red'>X</font>";}
$myURL = "playlist_new.php?show_id=$show_id&playlist_id=$playlist_id";

echo "<tr class = '$class' onMouseOver=\"this.className='index_table_o';\" onMouseOut=\"this.className='$class'\">
    <td width='25%'  onClick=\"window.location.href='$myURL'; \" ><a href='playlist_new.php?show_id=$show_id&playlist_id=$playlist_id'>$datetime</a></td>
    <td  style='padding: 2px;' onClick=\"window.location.href='$myURL'; \">$show_name</td>
    <td  style='padding: 2px;' onClick=\"window.location.href='$myURL'; \">&nbsp;</td>
	<td  style='padding: 2px;' onClick=\"window.location.href='$myURL'; \"># of tracks: $e</td>
	<td  style='padding: 2px;'><a href='javascript: validateDel($playlist_id);'>$delBut</a></td>
  </tr>";

}




?>  
  

</table>


<p>&nbsp; </p>
</body>
</html>
